package com.itcast.googleplay.holder;

import java.util.ArrayList;
import java.util.List;

import com.itcast.googleplay.PicActivity;
import com.itcast.googleplay.R;
import com.itcast.googleplay.domain.AppInfo;
import com.itcast.googleplay.http.CZApi;
import com.itcast.googleplay.utils.UIUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.content.Intent;
import android.sax.StartElementListener;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

public class AppPicHolder extends BaseHolder<AppInfo>{

	private LinearLayout llContainer;
	private DisplayImageOptions options;
	private AppInfo data;

	@Override
	public void initData() {
		data = getData();
		List<String> screen = data.screen;
		LayoutParams layoutParams = new LinearLayout.LayoutParams
			(LinearLayout.LayoutParams.WRAP_CONTENT, 
					LinearLayout.LayoutParams.WRAP_CONTENT);
		layoutParams.leftMargin = UIUtils.dip2px(6);
		layoutParams.topMargin = UIUtils.dip2px(6);
		layoutParams.bottomMargin = UIUtils.dip2px(6);
		
		for (int i = 0; i < screen.size(); i++) {
			ImageView imageView = new ImageView(UIUtils.getContext());
			if(!TextUtils.isEmpty(data.screen.get(i))){
				ImageLoader.getInstance().displayImage(CZApi.BASEIMGURL+data.screen.get(i), imageView,options);
			}
//			imageView.setLayoutParams(layoutParams);
			llContainer.addView(imageView,layoutParams);
			
			imageView.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(UIUtils.getContext(), PicActivity.class);
					intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					intent.putStringArrayListExtra("imageList",(ArrayList<String>) data.screen);
					UIUtils.getContext().startActivity(intent);
				}
			});
		}
	}

	@Override
	public View initView() {
		options = new DisplayImageOptions.Builder()
			.showImageOnLoading(R.drawable.ic_default)//在加载图片的时候,展示的默认图片
			
			.showImageForEmptyUri(R.drawable.ic_default)//uri传递出现问题时展示的图片
			
			.showImageOnFail(R.drawable.ic_default)//加载网络失败的时候,展示的图片
			
			.cacheInMemory(true)//是否需要内存缓存
			
			.cacheOnDisk(true)//是否需要磁盘缓存
			
			.considerExifParams(true)//是否支持图片自动调整,让其正常图像显示
	
			.build();//设置圆角半径
		
		View view = UIUtils.inflate(R.layout.layout_detail_app_pic);
		llContainer = (LinearLayout) view.findViewById(R.id.ll_container);
		return view;
	}

}
